package com.example.baselib.base;

import android.content.Context;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.Window;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.baselib.R;
import com.example.baselib.annotation.BindEventBus;
import com.example.baselib.receiver.NetworkChangeReceiver;
import com.example.baselib.utils.EventBusHelper;
import com.gyf.immersionbar.ImmersionBar;


public abstract class BaseActivity extends AppCompatActivity {

    public Context mContext;
    private NetworkChangeReceiver receiver;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //EventBus.getDefault().register(this);
        doBeforeSetcontentView();
        setContentView(getLayoutId());
        mContext = this;
        registerNetworkChangeReceiver();
        initView();
        initData();
    }

    //获取布局文件
    public abstract int getLayoutId();

    //初始化view
    public abstract void initView();

    protected abstract void initData();

    private void doBeforeSetcontentView() {
        // 把actvity放到application栈中管理
       // AppManager.getAppManager().addActivity(this);
        // 无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 设置竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
        //初始化沉浸式
        initImmersionBar();
    }

    /**
     * 初始化沉浸式
     */
    protected void initImmersionBar() {

        //设置共同沉浸式样式
        ImmersionBar.with(this)
                .statusBarColor(R.color.white)
                .statusBarDarkFont(true)
                .fitsSystemWindows(true)
                .navigationBarDarkIcon(true)
                .fullScreen(false)
                .init();

    }

    /**
     * 注册网络监听广播
     */
    private void registerNetworkChangeReceiver() {
        receiver = new NetworkChangeReceiver(this);
        IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(receiver, filter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (receiver != null) {
            unregisterReceiver(receiver);
            receiver.onDestroy();
            receiver = null;
        }
        // 取消注册
        if (this.getClass().isAnnotationPresent(BindEventBus.class)) {
            EventBusHelper.unregister(this);
        }

    }


}